/**
 * @author Jason
 * Address detail activity
 * Parameter needed: address model
 */
package com.cats.activity;

import android.os.Bundle;

import com.cats.R;
import com.cats.adapter.AddressListAdapter;
import com.cats.model.AddressModel;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class AddressDetailActivity extends BaseActivity implements
		android.view.View.OnClickListener, AdapterView.OnItemSelectedListener {

	private String array_spinner[];
	private AddressModel paraAddressModel = null;

	public AddressDetailActivity() {
		// TODO Auto-generated constructor stub
	}

	@SuppressWarnings("unchecked")
	protected void onCreate(Bundle savedInstanceState) {
		System.out.println("[AddressDetailActivity]onCreate");		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_address_detail);
		getActionBar().setDisplayHomeAsUpEnabled(true);
		setActionBarTitle("Address");
		
		Button saveButton = (Button) findViewById(R.id.btnSave);
		saveButton.setOnClickListener((android.view.View.OnClickListener) this);
		
		Button deleteButton = (Button) findViewById(R.id.btnDelete);
		deleteButton.setOnClickListener((android.view.View.OnClickListener) this);
		
		// Test
		array_spinner = new String[3];
		array_spinner[0] = "United States";
		array_spinner[1] = "Canada";
		array_spinner[2] = "Other";

		Spinner s = (Spinner) findViewById(R.id.countrySpinner);
		ArrayAdapter adapter = new ArrayAdapter(this,
				android.R.layout.simple_spinner_dropdown_item, array_spinner);
		s.setOnItemSelectedListener(this);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		s.setAdapter(adapter);
		
		/**
		 * get parameter address model from parent activity
		 */
		Bundle b = getIntent().getExtras(); 
		this.paraAddressModel = (AddressModel) b.getSerializable("ADDRESS");
		this.displayAddressContent(paraAddressModel);
	}
	
	/**
	 * asssemble address model to activity
	 */
	private void displayAddressContent(AddressModel addressModel) {
		EditText fullNameEditText = (EditText) findViewById(R.id.txtName);
		fullNameEditText.setText(addressModel.getFullName());
		
		EditText addressLine1 = (EditText) findViewById(R.id.txtAddressLine1);
		addressLine1.setText(addressModel.getAddressLine1());
		
		EditText addressLine2 = (EditText) findViewById(R.id.txtAddressLine2);
		addressLine2.setText(addressModel.getAddressLine2());
		
		EditText cityEditText = (EditText) findViewById(R.id.txtCity);
		cityEditText.setText(addressModel.getCity());
		
		EditText stateEditText = (EditText) findViewById(R.id.txtState);
		stateEditText.setText(addressModel.getState());
		
		EditText zipEditText = (EditText) findViewById(R.id.txtZipcode);
		zipEditText.setText(addressModel.getZipcode());
		
		EditText telephoneEditText = (EditText) findViewById(R.id.txtTelephone);
		telephoneEditText.setText(addressModel.getPhone());
		
		EditText emailEditText = (EditText) findViewById(R.id.txtEmail);
		emailEditText.setText(addressModel.getEmail());
		
		Spinner s = (Spinner) findViewById(R.id.countrySpinner);
		ArrayAdapter adapter = (ArrayAdapter)s.getAdapter();
		s.setSelection(adapter.getPosition(addressModel.getCountry()));
	}

	public void onItemSelected(AdapterView<?> parent, View v, int position,
			long id) {
		System.out.println("[NewAddressActivity]Select Country:"
				+ parent.getItemAtPosition(position).toString());
	}

	public void onNothingSelected(AdapterView<?> parent) {
	}

	private boolean varifyInputContent() {
		EditText nameEditText = (EditText) findViewById(R.id.txtName);
		String nameString = nameEditText.getText().toString();
		if (nameString.replaceAll("\\s+", "").length() == 0) {
			showWarningWithMsg("Please inpt your full name.");
			return false;
		} // 1. check name

		EditText addressEditText = (EditText) findViewById(R.id.txtAddressLine1);
		String addressString = addressEditText.getText().toString();
		if (addressString.replaceAll("\\s+", "").length() == 0) {
			showWarningWithMsg("Please inpt your address.");
			return false;
		} // 2. check address line 1

		EditText cityEditText = (EditText) findViewById(R.id.txtCity);
		String cityString = cityEditText.getText().toString();
		if (cityString.replaceAll("\\s+", "").length() == 0) {
			showWarningWithMsg("Please inpt your city.");
			return false;
		} // 3. check City

		EditText stateEditText = (EditText) findViewById(R.id.txtState);
		String stateString = stateEditText.getText().toString();
		if (stateString.replaceAll("\\s+", "").length() == 0) {
			showWarningWithMsg("Please inpt your state.");
			return false;
		} // 4. check state

		EditText zipEditText = (EditText) findViewById(R.id.txtZipcode);
		String zipString = zipEditText.getText().toString();
		if (zipString.replaceAll("\\s+", "").length() == 0) {
			showWarningWithMsg("Please inpt your zipcode.");
			return false;
		} // 5. check zipcode

		EditText telephoneEditText = (EditText) findViewById(R.id.txtTelephone);
		String telephoneString = telephoneEditText.getText().toString();
		if (telephoneString.replaceAll("\\s+", "").length() == 0) {
			showWarningWithMsg("Please inpt your telephone.");
			return false;
		} // 6. check telephone

		EditText emailEditText = (EditText) findViewById(R.id.txtEmail);
		String emailString = emailEditText.getText().toString();
		if (emailString.replaceAll("\\s+", "").length() == 0) {
			showWarningWithMsg("Please inpt your email address.");
			return false;
		} // 7. check email

		return true;
	}

//	private void showWarningWithMsg(String msg) {
//		AlertDialog.Builder builder = new AlertDialog.Builder(this);
//		builder.setMessage(msg).setCancelable(false)
//				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
//					public void onClick(DialogInterface dialog, int id) {
//						// do things
//					}
//				});
//		AlertDialog alert = builder.create();
//		alert.show();
//	}

	private void showConfirmWithMsg(String msg) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(msg).setCancelable(false)
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						/**
						 * delete address and finish
						 */
						AddressDetailActivity.this.finish();
						
					}
				})
				.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						/**
						 * do nothing if cancel
						 */
					}
				});
		AlertDialog alert = builder.create();
		alert.show();
	}

	/**
	 * @override
	 * save, delete button click callback
	 */
	public void onClick(View v) {
		// TODO: save address to server
		if (v.getId() == R.id.btnSave) {
			if (this.varifyInputContent()) {
				// TODO: save address to server
				finish();
			} else {
				// test
				// finish();
			}			
		}
		else if (v.getId() == R.id.btnDelete) {
			showConfirmWithMsg("Are you sure you want to delete this address?");
		}
	}
}
